/**
 * Copyright 厦门中软海晟信息技术有限公司 版权所有 违者必究 2019
 */
package com.leetCode.other.array.easy;

import java.util.ArrayList;
import java.util.List;

import static com.leetCode.Utils.printArray;

/**
 * @author : sunzb
 * @date: 2019/5/17
 */
public class SortArrayByParity_905 {

    public int[] sortArrayByParity1(int[] A) {
        int len = A.length;
        int[] res = new int[len];
        int left = 0; int right = len - 1;
        for (int i = 0; i < len; i++) {
            int value = A[i];
            if ((value & 1) == 0) res[left++] = value;
            else res[right--] = value;
        }
        return res;
    }

    public int[] sortArrayByParity(int[] A) {
        int len = A.length;
        List<Integer> odd = new ArrayList<>();
        List<Integer> even = new ArrayList<>();
        int value;
        for (int i = 0; i < len; i++) {
            value = A[i];
            if ((value & 1) == 0) even.add(value);
            else odd.add(value);
        }
        int[] res = new int[len];
        int offset = even.size();
        for (int i = 0; i < offset; i++)
            res[i] = even.get(i);
        for (int i = offset; i < len; i++)
            res[i] = odd.get(i - offset);
        return res;
    }

    public static void main(String[] args) {
        printArray(new SortArrayByParity_905().sortArrayByParity(
                new int[] {3,1,2,4}
        ));
    }
}
